package at.chl.lution.factory.spring;

import java.util.HashMap;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.config.MapFactoryBean;

import at.chl.genetic.function.math.random.MathRandomUtil;
import at.chl.genetic.function.math.request.MathFunctionRequest;
import at.chl.genetic.function.request.IGeneticFunctionRequest;
import at.chl.genetic.function.result.IGeneticFunctionResult;
import at.chl.genetic.node.IDecisionNode;
import at.chl.lution.creator.IExpectedResultCreator;
import at.chl.lution.creator.SimpleExpectedResultCreator;

public class MathExpectionsMapFactoryBean implements FactoryBean{

	private HashMap<IGeneticFunctionRequest, IGeneticFunctionResult> expectionsMap=new HashMap<IGeneticFunctionRequest, IGeneticFunctionResult>();
	private IExpectedResultCreator expectedResultCreator;
	private Long range=5l;
	
	public MathExpectionsMapFactoryBean(){
		this.expectedResultCreator=new SimpleExpectedResultCreator();
	}

	@Override
	public Object getObject() throws Exception {
		
		expectionsMap=new HashMap<IGeneticFunctionRequest, IGeneticFunctionResult>();
		
		for(int i=0;i<range;i++){
			IGeneticFunctionRequest request=createRandomRequest();
			expectionsMap.put(request, expectedResultCreator.getExpectedResult(request));
		}
		
		
		return expectionsMap;
	}

	private IGeneticFunctionRequest createRandomRequest() {
		MathFunctionRequest request=new MathFunctionRequest();
		request.setX(MathRandomUtil.getRandomLong());
		request.setY(MathRandomUtil.getRandomLong());
		
		return request;
	}

	public void setRange(Long range) {
		this.range = range;
	}

	@Override
	public Class getObjectType() {
		
		return expectionsMap.getClass();
	}

	@Override
	public boolean isSingleton() {
		return false;
	}
	public IExpectedResultCreator getExpectedResultCreator() {
		return expectedResultCreator;
	}

	public void setExpectedResultCreator(
			IExpectedResultCreator expectedResultCreator) {
		this.expectedResultCreator = expectedResultCreator;
	}

}
